const app = getApp(),url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // dateValue_s:'开始时间',
    // dateValue_e:'至今',
    // dateValue_s_v:'',
    // dateValue_d_v:'',
    honor:'',
    level:'',
    time:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log()
    that.setData({id:options.id});;
    if(options.id > 0){
      wx.request({
        url: url + 'cvSchoolHonor/getCVSchoolHonorById',
        method: "GET",
        data:{id:options.id },
        success:function(res){
          console.log("荣誉详情",res);
          var ret = res.data
          if(ret.code == 200){
            that.setData({
              id:ret.data.id,
              honor:ret.data.honor,
              level:ret.data.level,
              time:ret.data.time,
            })
          }else{
            that.setData({jldetail:''})
            wx.showToast({
              title: ret.msg,
              icon:'none'
            })
          }
        }
      })
    }else{
      console.log('=0')
      return false
    }
  },
  // 删除经历
  del(){
    var that = this
    var id = that.data.id
    wx.request({
      url: url + 'cvSchoolHonor/delete?id='+id,
      method: "DELETE",
      // data:{id:id },
      success:function(res){
        console.log("删除",res);
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title:'删除成功',
            icon:'none',
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
          
        }else{
          
          that.setData({sub:true})
          wx.showToast({
            title:'删除失败',
            icon:'none',
          })
        }
      }
    })
  },
  pjhonor(e){
    var that = this
    that.setData({honor:e.detail.value})
  },
  pjlevel(e){
    var that = this
    that.setData({level:e.detail.value})
  },
  bindDateChange: function(e) {
    console.log('time', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  
  checkSensWords(e){
    let itemData = e.target.dataset;
    let label = Object.values(itemData)[0]
    let key = Object.keys(itemData)[0]
    this.sensWords = this.sensWords || {}
    app.validSensWords(e.detail.value, label).then(() => {
      delete this.sensWords[key]
    }).catch(() => {
      this.sensWords = {...itemData, ...this.sensWords}
    })
  },
  // 提交
  savedata(e){
    var that = this
    if(that.data.honor == ''){
      wx.showToast({
        title: '请输入荣誉名称',
        icon:'none',
      })
      return false
    }
    if(that.data.level == ''){
      wx.showToast({
        title: '请输入荣誉等级',
        icon:'none',
      })
      return false
    }
    if(that.data.time == ''){
      wx.showToast({
        title: '请选择时间',
        icon:'none',
      })
      return false
    }
    let sensWords = Object.values(this.sensWords || {})
    if(sensWords.length > 0){
      wx.showToast({
        title: `${sensWords.join(',')}包含敏感词，请修改后再保存`,
        icon:'none',
      })
			return false;
    }

    var jiliid = that.data.id
    var uid = wx.getStorageSync('user_massage')
    var data = {
      "honor": that.data.honor,//	
      "level": that.data.level,//
      "time": that.data.time,//
      "userUid": uid.userUid,//用户编号
    }

    if(jiliid > 0){
      //编辑
      var method = 'PUT'
      var api = 'cvSchoolHonor/updateSchoolHonor'
      data.id = jiliid
    }else{
      // 添加
      var method = 'POST'
      var api = 'cvSchoolHonor/add'
    }

    wx.request({
      url:url + api,
      header:{'context-type': 'application/json',},
      method: method,
      data:data,
      success:function(res){
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title:'保存成功',
            icon:'none',
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
        }else{
          wx.showToast({
            title:'保存失败',
            icon:'none',
          })
        }
      }
    })

  },


})